package com.wwj.app.v1;

import com.github.pagehelper.Page;
import com.wwj.core.common.Constants;
import com.wwj.core.wawa.entity.PlayRecord;
import com.wwj.core.wawa.entity.Player;
import com.wwj.core.wawa.entity.Room;
import com.wwj.core.wawa.service.IPlayRecordService;
import com.wwj.core.wawa.service.IPlayerService;
import com.wwj.core.wawa.service.IRoomService;
import com.wwj.framework.controller.BaseController;
import com.wwj.framework.utils.ReturnData;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/11/3.
 */
@Controller
@RequestMapping("/v1/room")
public class RoomController extends BaseController {

    @Resource
    private IRoomService roomService;

    @Resource
    private IPlayRecordService playRecordService;

    @Resource
    private IPlayerService playerService;

    @Resource
    private JedisPool jedisPool;


    @RequestMapping("/list")
    @ResponseBody
    public ReturnData list(@RequestParam(required = false) Integer pageNo, @RequestParam(required = false) Integer pageSize) {
        ReturnData returnData = new ReturnData();
        try {
            if(pageNo == null || pageNo == 0) {
                pageNo = 1;
            }
            if(pageSize == null || pageSize ==0) {
                pageSize = 10;
            }
            int offset = (pageNo - 1) * pageSize;
            int limit = pageSize;
            Page<Room> rooms = roomService.findByPage(offset, limit);
            returnData.success(rooms);
        } catch (Exception e) {
            logger.error("", e);
            returnData.failure("系统错误,请联系客服");
        }
        return returnData;
    }

    @RequestMapping("/{id}")
    @ResponseBody
    public ReturnData roomInfo(@PathVariable int id) {
        ReturnData returnData = new ReturnData();
        try {
            Room room = roomService.findById(id);
            if (room == null) {
                returnData.failure("找不到房间信息!");
                return returnData;
            }

            returnData.success(room);
        } catch (Exception e) {
            logger.error("", e);
            returnData.failure("系统错误,请联系客服");
        }
        return returnData;
    }

    @RequestMapping("/{id}/status")
    @ResponseBody
    public ReturnData roomStatus(@PathVariable int id) {
        ReturnData returnData = new ReturnData();
        try {
            Room room = roomService.findById(id);
            if (room == null) {
                returnData.failure("找不到房间信息!");
                return returnData;
            }
            returnData.success(room.getWaWaMachine().getStatus());
        } catch (Exception e) {
            logger.error("", e);
            returnData.failure("系统错误,请联系客服");
        }
        return returnData;
    }

    @RequestMapping("/{id}/playRecords")
    @ResponseBody
    public ReturnData listPlayRecord(@PathVariable int id, @RequestParam int playResult, @RequestParam(required = false) Integer pageNo, @RequestParam(required = false) Integer pageSize) {
        ReturnData returnData = new ReturnData();
        try {
            if(pageNo == null || pageNo == 0) {
                pageNo = 1;
            }
            if(pageSize == null || pageSize ==0) {
                pageSize = 10;
            }
            Room room = roomService.findById(id);
            if (room == null) {
                returnData.failure("找不到房间信息!");
                return returnData;
            }
            Map<String, Object> params = new HashMap<String, Object>();
            params.put("roomId", id);
            if(playResult != 0) {
                params.put("playResult", playResult);
            }
            int offset = (pageNo - 1) * pageSize;
            int limit = pageSize;
            Page<PlayRecord> playRecords = playRecordService.findByPage(params, offset, limit);
            returnData.success(playRecords);
        } catch (Exception e) {
            logger.error("", e);
            returnData.failure("系统错误,请联系客服");
        }
        return returnData;
    }

    @RequestMapping("/{id}/players")
    @ResponseBody
    public ReturnData listPlayers(@PathVariable int id) {
        ReturnData returnData = new ReturnData();
        try {
            Room room = roomService.findById(id);
            if (room == null) {
                returnData.failure("找不到房间信息!");
                return returnData;
            }
            List<Player> players = playerService.findByRoomId(id);
            returnData.success(players);
        } catch (Exception e) {
            logger.error("", e);
            returnData.failure("系统错误,请联系客服");
        }
        return returnData;
    }

    @RequestMapping("/{id}/playerCountInQueue")
    @ResponseBody
    public ReturnData playerCountInQueue(@PathVariable int id) {
        ReturnData returnData = new ReturnData();
        try {
            Room room = roomService.findById(id);
            if (room == null) {
                returnData.failure("找不到房间信息!");
                return returnData;
            }
            Jedis jedis = jedisPool.getResource();
            long count = jedis.zcard(Constants.KEY_ROOM_QUEUE + id);
            jedis.close();
            returnData.success(count);
        } catch (Exception e) {
            logger.error("", e);
            returnData.failure("系统错误,请联系客服");
        }
        return returnData;
    }
}
